package com.teacher.server;

import com.teacher.entity.Student;
import com.teacher.entity.Teacher;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * ServerService--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-09-25 16:23
 **/
public class ServerService {

    private static Teacher teacher = null;

    private static List<Student> students = null;

    static {

        teacher = new Teacher(1, "admin", "123", "Mr ma");

        students = new CopyOnWriteArrayList<>();
        students.add(new Student(1, "zhangsan", 1, 22, 1, "good"));
        students.add(new Student(2, "lisi", 1, 22, 1, "good1"));
        students.add(new Student(3, "wangwu", 1, 22, 0, "good2"));
        students.add(new Student(4, "zhaoliu", 2, 22, 1, "good3"));
    }

    public Teacher doLogin(String userName, String password) {

        if (userName.equalsIgnoreCase(teacher.getUserName()) &&
                password.equals(teacher.getPassWord())) {
            return teacher;
        } else {
            return null;
        }
    }

    public List<Student> findStudentByTeacher(int id) {

        List<Student> stuList = new ArrayList<>();
        for (Student student : students) {
            if (id == student.getTeacherId()) {
                stuList.add(student);
            }
        }

        return stuList;
    }

    public void deleteStudent(int id) {

        for (Student student : students) {
            if (student.getStuId() == id) {
                students.remove(student);
            }
        }

    }

    public void updateStudent(Student student) {

        for (Student student1 : students) {

            if (student.getStuId() == student1.getStuId()) {
                student1.setComment(student.getComment());
            }
        }
    }

}
